package com.example.administrator.managesystem.view;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Parcel;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.utils.DisplayUtils;
import com.example.administrator.managesystem.utils.DpUtils;

import me.jessyan.autosize.utils.LogUtils;


/**
 * 作者：Created by wr
 * 时间: 2017/12/19 10:53
 */
public class BaseBottomDialog extends MyDialogFragment {

    protected Dialog mDialog;
    protected String mTitle;
    protected View mParentView;
    protected Builder mBuilder;


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = MyCreateDialog(R.style.bottom_dialog_style);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mBuilder = getArguments().getParcelable("builder");


        mParentView = LayoutInflater.from(getContext()).inflate(mBuilder.layoutId, null, false);

        mDialog.setContentView(mParentView);
        mTitle = mBuilder.title;
        initData();
        initListener();
        int dip2px = DpUtils.dip2px(getAttachActivity(), mBuilder.interval);
        mDialog.getWindow().getDecorView().setPadding(dip2px, 0, dip2px, 0);

        // 在底部，宽度撑满
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM | Gravity.CENTER;
        int screenWidth = getWidth(getAttachActivity());
        int screenHeight = getHeight(getAttachActivity());
        params.width = screenWidth < screenHeight ? screenWidth : screenHeight;
        mDialog.getWindow().setAttributes(params);
        mDialog.setCanceledOnTouchOutside(mBuilder.cancelOnTouch);
//        mDialog.getWindow().setDimAmount(builder.cover);//核心代码 解决了无法去除遮罩

        return mDialog;
    }

    protected void initListener() {
    }

    protected void initData() {
    }

    public static class Builder extends BaseDialogBuilder {
        public Builder() {
        }

        public Builder(Parcel in) {
            super(in);
        }
    }
}
